<h3>Pool Statistics</h3>

<!-- Statistics -->
<div id="poolStats" class="row">

    <!-- Total Mined -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-money"></span>
            </div>
            <div class="content">
                <div class="text">Total Mined</div>
                <div class="value"><span id="totalMined">...</span></div>
            </div>
        </div>
    </div>

    <!-- Total Paid -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-money"></span>
            </div>
            <div class="content">
                <div class="text">Total Paid</div>
                <div class="value"><span id="totalPaid">...</span></div>
            </div>
        </div>
    </div>

    <!-- Total Owed -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-money"></span>
            </div>
            <div class="content">
                <div class="text">Total Owed</div>
                <div class="value"><span id="totalOwed">...</span></div>
            </div>
        </div>
    </div>
    
    <!-- Profit (before tx fees) -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-dollar"></span>
            </div>
            <div class="content">
                <div class="text">Profit (before tx fees)</div>
                <div class="value"><span id="profit">...</span></div>
            </div>
        </div>
    </div>
    
    <!-- Registered Addresses -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-users"></span>
            </div>
            <div class="content">
                <div class="text">Registered Addresses</div>
                <div class="value"><span id="registeredAddresses">...</span></div>
            </div>
        </div>
    </div>

    <!-- Blocks Unlocked -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-cubes"></span>
            </div>
            <div class="content">
                <div class="text">Blocks Unlocked</div>
                <div class="value"><span id="blocksUnlocked">...</span></div>
            </div>
        </div>
    </div>

    <!-- Blocks Orphaned -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-cubes"></span>
            </div>
            <div class="content">
                <div class="text">Blocks Orphaned</div>
                <div class="value"><span id="blocksOrphaned">...</span> <span class="smallText">(<span id="orphanPercent">0%</span>)</span></div>
            </div>
        </div>
    </div>
    
    <!-- Average Luck (shares/diff) -->
    <div class="col-lg-3 col-sm-4">
        <div class="infoBox hoverExpandEffect">
            <div class="icon">
                <span class="fa fa-line-chart"></span>
            </div>
            <div class="content">
                <div class="text">Average Luck (shares/diff)</div>
                <div class="value"><span id="averageLuck">...</span></div>
            </div>
        </div>
    </div>
        
</div>

<!-- Javascript -->
<script>
function formatLuck(difficulty, shares) {
    // Only an approximation to reverse the calculations done in pool.js, because the shares with their respective times are not recorded in redis
    // Approximation assumes equal pool hashrate for the whole round
    // Could potentially be replaced by storing the sum of all job.difficulty in the redis db.
    if (lastStats.config.slushMiningEnabled) {
        // Uses integral calculus to calculate the average of a dynamic function
        var accurateShares = 1/lastStats.config.blockTime * (  // 1/blockTime to get the average
            shares * lastStats.config.weight * (                  // Basically calculates the 'area below the graph' between 0 and blockTime
                1 - Math.pow( 
                    Math.E, 
                    ((- lastStats.config.blockTime) / lastStats.config.weight)  // blockTime is equal to the highest possible result of (dateNowSeconds - scoreTime)
                )
            )
        );
    }
    else {
        var accurateShares = shares;
    }
	    
    var percent = Math.round(accurateShares / difficulty * 100);
    if(!percent){
        return '<span class="luckGood">?</span>';
    }
    else if(percent <= 100){
        return '<span class="luckGood">' + percent + '%</span>';
    }
    else if(percent >= 101 && percent <= 150){
        return '<span class="luckMid">' + percent + '%</span>';
    }
    else{
        return '<span class="luckBad">' + percent + '%</span>';
    }	
}

function getStats(promptPassword) {
    var password = docCookies.getItem('password');

    if(!password || promptPassword) {
        password = prompt('Enter admin password');
    }

    $('#loading').show();
    $.ajax({
        url: api + '/admin_stats',
        data: {password: password},
	dataType: 'json',
        cache: 'false',
        success: function(data) {
            docCookies.setItem('password', password, Infinity);
            $('#loading').hide();
            renderData(data);
        },
        error: function(e) {
            docCookies.removeItem('password');
            getStats(true);
        }
    });
}

function renderData(data) {
    $('#totalOwed').text(getReadableCoins(data.totalOwed, 4, true));
    $('#totalPaid').text(getReadableCoins(data.totalPaid, 4, true));
    $('#totalMined').text(getReadableCoins(data.totalRevenue, 4, true));
    $('#profit').text(getReadableCoins(data.totalRevenue - data.totalOwed - data.totalPaid, 4, true));
    $('#blocksUnlocked').text(data.blocksUnlocked);
    $('#averageLuck').html(formatLuck(data.totalDiff, data.totalShares));
    $('#blocksOrphaned').text(data.blocksOrphaned);
    $('#orphanPercent').text((data.blocksOrphaned / data.blocksUnlocked * 100).toFixed(2) + '%');
    $('#registeredAddresses').text(data.totalWorkers);
}

$(function() {
    getStats();
});
</script>
